﻿//////////////////////////////////////////////////////////////////////////
// FileName:fmBusApplyForm.aspx
// CreateBy:DragonXiong
// CreateDate:2010-05-22
// Description:
//		Bus Apply Form
//      
// Modify Log:
//	
// Copyright:
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Web.Common;
using Auto.Components;
using Model = Auto.Data.BusMan.Model;
using BLL = Auto.Data.BusMan.BLL;

namespace Auto.BusMan
{
    public partial class fmCarApplyForm : System.Web.UI.Page
    {
        public string DocID
        {
            get
            {
                ViewState["DocID"] = Request.QueryString["DocID"];
                return (ViewState["DocID"] == null) ? "" : ViewState["DocID"].ToString();
            }
            set { ViewState["DocID"] = value; }
        }

        Model.BusApply model = new Model.BusApply();
        BLL.BusApply bll = new BLL.BusApply();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            if (string.IsNullOrEmpty(DocID)) //新增
            {
                this.chkAddContinue.Checked = true;
                checktr.Visible = true;
                txtApplyDate.CalendarDate = DateTime.Now;
                txtApplyer.Text = SessionBox.User.UserName;
                hfUserID.Value = SessionBox.User.UserID;
                checktr.Attributes.Add("style", "display:block;");
                InitControl();
            }
            else  //修改
            {
                checktr.Attributes.Add("style", "display:none;");
                BindData();
            }
        }

        private void InitControl()
        {
            this.ucDriveToPhraseDropDownList.InitControl();
            this.ucWorkTaskPhraseDropDownList.InitControl();
            this.ucDDLCarSelector1.InitControl();
            this.UcLeaderPhraseDropDownList.InitControl();
        }

        private void ClearText()
        {
            this.ucDDLCarSelector1.SelectedValue = "";// this.txtBusIdentificationNO.Text = "";// model.BusID;
            //this.txtApplyer.Text = "";// model.Applyer;
            //this.txtApplyDate.CalendarDate = null;// model.ApplyDate;
            this.ucDriveToPhraseDropDownList.ClearSelectItem(); //this.ucDriveToPhraseDropDownList.SelectedValue = "";// this.txtDriveTo.Text = "";//model.DriveTo;
            this.txtDriveTo.Text = "";
            //this.txtWorkTaskID.Text = model.WorkTaskID;
            this.ucWorkTaskPhraseDropDownList.ClearSelectItem();// this.ucWorkTaskPhraseDropDownList.SelectedValue = "";// this.txtWorkTask.Text = "";// model.WorkTaskDesc;
            this.txtWorkTask.Text = "";
            this.txtPlanToTime.CalendarDate = null;// model.PlanToTime;
            this.txtPlanBackTime.CalendarDate = null;// model.PlanBackTime;
            this.UcLeaderPhraseDropDownList.SelectedValue = "";// this.txtApprovedBy.Text = "";// model.ApprovedBy;
            this.txtApprovedNote.Text = "";
            //this.txtApprovedNote.Text = model.ApprovedNote;
        }

        private void BindData()
        {
            InitControl();
            model = bll.GetModel(DocID);
            Session["fmCarApplyForm.Model"] = model;
            this.ucDDLCarSelector1.SelectedValue = model.BusID;// this.txtBusIdentificationNO.Text = model.BusID;
            this.txtApplyer.Text = new Auto.Data.Security.Users(model.Applyer).UserName;
            this.hfUserID.Value = model.Applyer;
            this.txtApplyDate.CalendarDate = model.ApplyDate;
            this.ucDriveToPhraseDropDownList.SelectedValue = model.DriveTo;// this.txtDriveTo.Text = model.DriveTo;
            this.txtDriveTo.Text = model.DriveToOther;
            //this.txtWorkTaskID.Text = model.WorkTaskID;
            this.ucWorkTaskPhraseDropDownList.SelectedValue = model.WorkTaskID;// this.txtWorkTask.Text = model.WorkTaskDesc;
            this.txtWorkTask.Text = model.WorkTaskOther;
            this.txtPlanToTime.CalendarDate = model.PlanToTime;
            this.txtPlanBackTime.CalendarDate = model.PlanBackTime;
            this.UcLeaderPhraseDropDownList.SelectedValue = model.ApprovedBy; //this.txtApprovedBy.Text = model.ApprovedBy;
            this.txtApprovedNote.Text = model.ApprovedNote;
            //this.txtApprovedNote.Text = model.ApprovedNote;
        }

        private Model.BusApply GetApplyForm()
        {
            if (!string.IsNullOrEmpty(DocID))
            {
                model = (Model.BusApply)Session["fmCarApplyForm.Model"];
            }
            //Tab1
            string BusID = this.ucDDLCarSelector1.SelectedValue;// this.txtBusIdentificationNO.Text;// this.txtBus.Text;
            string Applyer = this.hfUserID.Value;// this.txtApplyer.Text;
            DateTime ApplyDate = DateTime.Parse(this.txtApplyDate.Text);
            string DriveTo = this.ucDriveToPhraseDropDownList.SelectedValue;// this.txtDriveTo.Text;
            string DriveToDesc = this.ucDriveToPhraseDropDownList.SelectedText;
            string DriveToOther = this.txtDriveTo.Text.Trim();
            string WorkTaskID = this.ucWorkTaskPhraseDropDownList.SelectedValue;// this.txtWorkTask.Text; //this.txtWorkTaskID.Text;
            string WorkTaskDesc = this.ucWorkTaskPhraseDropDownList.SelectedText;// this.txtWorkTask.Text;// this.txtWorkTaskDesc.Text;
            string WorkTaskOther = this.txtWorkTask.Text.Trim();
            DateTime PlanToTime = DateTime.Parse(this.txtPlanToTime.Text);
            DateTime PlanBackTime = DateTime.Parse(this.txtPlanBackTime.Text);
            string ApprovedBy = this.UcLeaderPhraseDropDownList.SelectedValue;// this.txtApprovedBy.Text;
            string ApprovedNote = this.txtApprovedNote.Text;// this.txtApprovedBy.Text;// this.txtApprovedNote.Text;

            //Model.BusApply model = new Model.BusApply();
            //Tab1
            model.BusID = BusID;
            model.Applyer = Applyer;
            model.ApplyDate = ApplyDate;
            model.DriveTo = DriveTo;
            model.DriveToDesc = DriveToDesc;
            model.DriveToOther = DriveToOther;
            model.WorkTaskID = WorkTaskID;
            model.WorkTaskDesc = WorkTaskDesc;
            model.WorkTaskOther = WorkTaskOther;
            model.PlanToTime = PlanToTime;
            model.PlanBackTime = PlanBackTime;
            model.ApprovedBy = ApprovedBy;
            model.ApprovedNote = ApprovedNote;

            return model;


        }

        private bool Check()
        {
            string strErr = "";

            if (ucDDLCarSelector1.SelectedValue == "")
            {
                strErr += "请选择车辆!\\n";
            }
            if (ucDriveToPhraseDropDownList.SelectedValue == "" && txtDriveTo.Text.Trim()=="")
            {
                strErr += "请选择出车地!\\n";
            }
            if (ucWorkTaskPhraseDropDownList.SelectedValue == "" && txtWorkTask.Text.Trim()=="")
            {
                strErr += "请选择工作任务!\\n";
            }
            //if (UcLeaderPhraseDropDownList.SelectedValue == "")
            //{
            //    strErr += "请选择领导!\\n";
            //}
            if (strErr != "")
            {
                Jscript.AjaxAlert(this, strErr);
                return false;
            }
            else
            {
                return true;
            }
        }

        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Check())
            {
                return;
            }

            try
            {
                model = GetApplyForm();
                model.RecDivID = SessionBox.User.DivID;
                model.UserAcct = SessionBox.User.UserID;

                if (string.IsNullOrEmpty(DocID))
                {
                    bll.Add(model);
                }
                else
                {
                    bll.Update(model);
                }
                Redirect();

            }
            catch (Exception ex)
            {
                Jscript.AjaxAlert(this, ex.Message);
            }

        }
        private void Redirect()
        {
            if (chkAddContinue.Checked)
            {
                lblMsg.Text = "保存成功，请继续添加！";
                ClearText();
            }
            else
            {
                Server.Transfer("fmCarApplyList.aspx");
            }
        }

        #endregion Event
    }
}